package com.wing.wecom.service;

import chat.qiye.wechat.sdk.api.oa.OaCheckInApi;
import chat.qiye.wechat.sdk.api.oa.param.AddCheckInUserFaceParam;
import chat.qiye.wechat.sdk.api.oa.param.GetCheckInDataParam;
import chat.qiye.wechat.sdk.api.oa.param.GetCheckInUserRuleParam;
import chat.qiye.wechat.sdk.api.oa.param.SetCheckInScheduleListParam;
import chat.qiye.wechat.sdk.api.oa.resp.*;
import chat.qiye.wechat.sdk.common.RespStatus;
import chat.qiye.wechat.sdk.constant.AppTypeEnum;
import chat.qiye.wechat.sdk.service.ApiConfigurationDefaultProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 企业微信打卡-ContactUserServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CheckInServiceImpl implements CheckInService {

    @Resource
    OaCheckInApi oaCheckInApi;
    @Resource
    ApiConfigurationDefaultProvider apiConfigurationDefaultProvider;

    @Override
    public CorpCheckInRuleResp getCorpOption() {
        CorpCheckInRuleResp corpCheckInRuleResp = oaCheckInApi.getCorpOption();
        if(corpCheckInRuleResp.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            corpCheckInRuleResp = oaCheckInApi.getCorpOption();
        }
        return corpCheckInRuleResp;
    }

    @Override
    public UserCheckInRuleResp getUserOption(GetCheckInUserRuleParam param) {
        UserCheckInRuleResp userCheckInRuleResp = oaCheckInApi.getUserOption(param);
        if(userCheckInRuleResp.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            userCheckInRuleResp = oaCheckInApi.getUserOption(param);
        }
        return userCheckInRuleResp;
    }

    @Override
    public CheckInDataResp getData(GetCheckInDataParam param) {
        CheckInDataResp checkInDataResp = oaCheckInApi.getData(param);
        if(checkInDataResp.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            checkInDataResp = oaCheckInApi.getData(param);
        }
        return checkInDataResp;
    }

    @Override
    public CheckInDayDataResp getDayData(GetCheckInDataParam param) {
        CheckInDayDataResp checkInDayDataResp = oaCheckInApi.getDayData(param);
        if(checkInDayDataResp.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            checkInDayDataResp = oaCheckInApi.getDayData(param);
        }
        return checkInDayDataResp;
    }

    @Override
    public CheckInMonthDataResp getMonthData(GetCheckInDataParam param) {
        CheckInMonthDataResp checkInMonthDataResp = oaCheckInApi.getMonthData(param);
        if(checkInMonthDataResp.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            checkInMonthDataResp = oaCheckInApi.getMonthData(param);
        }
        return checkInMonthDataResp;
    }

    @Override
    public CheckInUserScheduleListResp getUserScheduleList(GetCheckInDataParam param) {
        CheckInUserScheduleListResp checkInUserScheduleListResp = oaCheckInApi.getUserScheduleList(param);
        if(checkInUserScheduleListResp.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            checkInUserScheduleListResp = oaCheckInApi.getUserScheduleList(param);
        }
        return checkInUserScheduleListResp;
    }

    @Override
    public int setUserScheduleList(SetCheckInScheduleListParam param) {
        RespStatus respStatus = oaCheckInApi.setUserScheduleList(param);
        if(respStatus.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            respStatus = oaCheckInApi.setUserScheduleList(param);
        }
        return respStatus.success() ? 1 : 0;
    }

    @Override
    public int addUserFace(AddCheckInUserFaceParam param) {
        RespStatus respStatus = oaCheckInApi.addUserFace(param);
        if(respStatus.getErrcode() == 42001){ // access_token已过期
            apiConfigurationDefaultProvider.loadAppToken(AppTypeEnum.APP_CHECK_IN.getAppId());
            respStatus = oaCheckInApi.addUserFace(param);
        }
        return respStatus.success() ? 1 : 0;
    }

}
